package weaver.jinpin.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.google.gson.Gson;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

public class WorkflowToK3Cloud extends BaseBean implements Action {
	/* 日志 */
	private Logger logger = Logger.getLogger(WorkflowToK3Cloud.class);
	
	public String execute(RequestInfo request) {
		String src = request.getRequestManager().getSrc();
		if ("submit".equals(src)) {
			// 获取流程数据
			RequestMsg info = createRequestMsg(request);
			// 
			Gson gson = new Gson();
			String json = gson.toJson(info);
			// 写数据
			saveRequestMsg(info);
			// 写日志
			String msg = String.format("K3Cloud Workflow Request %s", json);
			logger.info(msg);
		}
		return Action.SUCCESS;
	}

	private void saveRequestMsg(RequestMsg info) {
		RecordSet rs = new RecordSet();
		String sql = "INSERT INTO workflow_k3cloud (objtype, objid, workflowid, requestid, creatorid, description) VALUES (?, ?, ?, ?, ?, ?)";
		List<Object> var = new ArrayList<Object>();
		var.add(info.getObjtype());
		var.add(info.getObjid());
		var.add(Integer.parseInt(info.getWorkflowid()));
		var.add(Integer.parseInt(info.getRequestid()));
		var.add(info.getCreatorid());
		var.add(info.getDescription());
		rs.executeUpdate(sql, var.toArray());
	}

	private RequestMsg createRequestMsg(RequestInfo request) {
		RequestMsg info = new RequestMsg();
		info.setObjid(request.getObjid());
		info.setObjtype(request.getObjtype());
		info.setIspreadd(request.getIspreadd());
		info.setSecLevel(request.getSecLevel());
		info.setSecValidity(request.getSecValidity());
		info.setWorkflowid(request.getWorkflowid());
		info.setRequestid(request.getRequestid());
		info.setCreatorid(request.getCreatorid());
		info.setDescription(request.getDescription());
		info.setRequestlevel(request.getRequestlevel());
		info.setRemindtype(request.getRemindtype());
		info.setHostid(request.getHostid());
		info.setLastoperator(request.getLastoperator());
		info.setRemark(request.get_Remark());
		info.setHostid(request.getHostid());
		info.setIsNextFlow(request.getIsNextFlow());
		return info;
	}
}

class RequestMsg {
	private int objtype;
	private int objid;
	private String ispreadd;
	private String secLevel = "";
	private String secValidity = "";
	private String workflowid;
	private String requestid;
	private String creatorid;
	private String description;
	private String requestlevel;
	private String remindtype;
	private String hostid;
	private String lastoperator;
	private String Remark;
	private String isNextFlow;

	public int getObjtype() {
		return objtype;
	}

	public void setObjtype(int objtype) {
		this.objtype = objtype;
	}

	public int getObjid() {
		return objid;
	}

	public void setObjid(int objid) {
		this.objid = objid;
	}

	public String getIspreadd() {
		return ispreadd;
	}

	public void setIspreadd(String ispreadd) {
		this.ispreadd = ispreadd;
	}

	public String getSecLevel() {
		return secLevel;
	}

	public void setSecLevel(String secLevel) {
		this.secLevel = secLevel;
	}

	public String getSecValidity() {
		return secValidity;
	}

	public void setSecValidity(String secValidity) {
		this.secValidity = secValidity;
	}

	public String getWorkflowid() {
		return workflowid;
	}

	public void setWorkflowid(String workflowid) {
		this.workflowid = workflowid;
	}

	public String getRequestid() {
		return requestid;
	}

	public void setRequestid(String requestid) {
		this.requestid = requestid;
	}

	public String getCreatorid() {
		return creatorid;
	}

	public void setCreatorid(String creatorid) {
		this.creatorid = creatorid;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getRequestlevel() {
		return requestlevel;
	}

	public void setRequestlevel(String requestlevel) {
		this.requestlevel = requestlevel;
	}

	public String getRemindtype() {
		return remindtype;
	}

	public void setRemindtype(String remindtype) {
		this.remindtype = remindtype;
	}

	public String getHostid() {
		return hostid;
	}

	public void setHostid(String hostid) {
		this.hostid = hostid;
	}

	public String getLastoperator() {
		return lastoperator;
	}

	public void setLastoperator(String lastoperator) {
		this.lastoperator = lastoperator;
	}

	public String getRemark() {
		return Remark;
	}

	public void setRemark(String remark) {
		Remark = remark;
	}

	public String getIsNextFlow() {
		return isNextFlow;
	}

	public void setIsNextFlow(String isNextFlow) {
		this.isNextFlow = isNextFlow;
	}
}